/*
 * @Author: 火红色祥云 809880192@qq.com
 * @Date: 2023-05-26 09:55:13
 * @LastEditors: 火红色祥云 809880192@qq.com
 * @LastEditTime: 2023-05-26 17:08:25
 * @FilePath: /imx6ull-app/30_MQTT/mqttClient.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>

#include "/home/lux/Downloads/MQTT/paho.mqtt.c-1.3.8/_install/include/MQTTClient.h"

const char serverURL[]="tcp://test.ranye-iot.net:1883";         //MQTT服务器地址
const char clientID[]="Lux19357168824";       //客户端ID

#define TOPIC_WILL  "lux_topic/will"
#define TOPIC_LED   "lux_topic/led"
#define TOPIC_TEMP  "lux_topic/temp"

//客户端断开连接后的回调函数
static  void CallBack_connectionLost(void *context, char *cause)
{
    printf(" void MQTTClient_connectionLost()\r\n");
    printf("cause:%s\r\n",cause);
}
//接收到消息的回调函数
static  int CallBack_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    if (!strcmp(topicName,TOPIC_LED))
    {
        
    }
    printf("topicName:%s\r\n",topicName);
    printf("message:%s\r\n",message->payload);

    return 1;
}
int main(int argc, char const *argv[])
{
    MQTTClient client;                                                                  //客户端句柄
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;        //连接操作对象
    MQTTClient_willOptions will_opts = MQTTClient_willOptions_initializer;              //遗嘱对象
    MQTTClient_message pubmsg = MQTTClient_message_initializer;                         //消息对象

    int rc;

    //创建客户端
    if(MQTTCLIENT_SUCCESS != (rc=MQTTClient_create(&client,serverURL,clientID,MQTTCLIENT_PERSISTENCE_NONE,NULL)))
    {
        printf("Failed to create client! error code:%d\r\n",rc);
        rc= EXIT_FAILURE;
        goto exit;
    }

    //设置回调函数
    if(MQTTCLIENT_SUCCESS != (rc = MQTTClient_setCallbacks(client,NULL,CallBack_connectionLost,CallBack_messageArrived,NULL)))
    {
        printf("Failed to set callbacks,error code:%d\r\n",rc);
        rc = EXIT_FAILURE;
        goto exit;
    }

    //连接MQTT服务器
    //遗嘱
    will_opts.topicName = TOPIC_WILL;
    will_opts.message = "unexpected disconnection";
    will_opts.retained = 1;                         //保留消息
    will_opts.qos = 0;
    //连接
    conn_opts.will = &will_opts;
    conn_opts.keepAliveInterval = 30;               //心跳包间隔时间
    conn_opts.cleansession = 0;
    //conn_opts.username = ;
    //conn_opts.password = ;
    if(MQTTCLIENT_SUCCESS != (rc = MQTTClient_connect(client,&conn_opts)))
    {
        printf("Failed to connect, error code:%d\r\n",rc);
        rc = EXIT_FAILURE;
        goto destory_exit;
    }
    printf("MQTT 连接成功！！！\r\n");

    //发布消息
    pubmsg.payload = "online";
    pubmsg.payloadlen = 6;
    pubmsg.qos = 0;
    pubmsg.retained = 1;                            //保留消息
    if(MQTTCLIENT_SUCCESS != (rc = MQTTClient_publishMessage(client,TOPIC_WILL,&pubmsg,NULL)))
    {
        printf("Failed to set MQTTClient_publishMessage,error code:%d\r\n",rc);
        rc = EXIT_FAILURE;
        goto disconnect_exit;
    }


    //订阅主题
    if(MQTTCLIENT_SUCCESS != (rc = MQTTClient_subscribe(client,TOPIC_LED,0)))
    {
        printf("Failed to set MQTTClient_subscribe,error code:%d\r\n",rc);
        rc = EXIT_FAILURE;
        goto disconnect_exit;
    }
    printf("成功订阅主题：%s\r\n",TOPIC_LED);

    while (1)
    {
        MQTTClient_message tempmsg = MQTTClient_message_initializer;

        char temp_str[10] = {0};
        int fd;


        fd = open("/sys/class/thermal/thermal_zone0/temp",O_RDONLY);
        read(fd,temp_str,sizeof(temp_str));
        close(fd);

        //发布温度
        tempmsg.payload = temp_str;
        tempmsg.payloadlen = strlen(temp_str);
        tempmsg.qos = 0;
        tempmsg.retained = 1;

        if(MQTTCLIENT_SUCCESS != (rc = MQTTClient_publishMessage(client,TOPIC_TEMP,&tempmsg,NULL)))
        {
            printf("Failed to set MQTTClient_subscribe,error code:%d\r\n",rc);
            rc = EXIT_FAILURE;
            goto unsubscribe_exit;
        }   
        printf("发布主题：%s:%s\r\n",TOPIC_TEMP,temp_str);
        sleep(30);     
    }

unsubscribe_exit:
    if(MQTTCLIENT_SUCCESS != (rc = MQTTClient_unsubscribe(client,TOPIC_LED)))
    {
        printf("Failed to set MQTTClient_unsubscribe,error code:%d\r\n",rc);
        rc = EXIT_FAILURE;
    }

disconnect_exit:
    if (MQTTCLIENT_SUCCESS != (rc = MQTTClient_disconnect(client,10000)))
    {
        printf("Failed to set MQTTClient_disconnect,error code:%d\r\n",rc);
        rc = EXIT_FAILURE;
    }        

destory_exit:
    MQTTClient_destroy(&client);

exit:
    return rc;

    return 0;
}

